﻿#pragma once

#include "lp_init.h"
#include "lp_socketpoll.h"

#ifndef LP_WINDOWS
#include <arpa/inet.h>
#endif

enum {
    LPSOCKET_CONNECT,
    LPSOCKET_ACCEPT,
    LPSOCKET_READ,
    LPSOCKET_ERROR,
};

enum {
    LPSOCKET_IPV0,
    LPSOCKET_IPV4,
    LPSOCKET_IPV6,
};

enum {
    LPSOCKET_TCP,
    LPSOCKET_UDP,
};


typedef struct LPSocketAttacher LPSocketAttacher;
typedef LPHandle LPSocket;

typedef union {
    struct sockaddr s;
    struct sockaddr_in v4;
    struct sockaddr_in6 v6;
}LPSocketAddr;

typedef struct {
    char ip[128];
    uint16_t port;
    int protocol;
    int family;
}LPSocketInfo;

typedef struct {
    LPSocket so;
    char* data;
    size_t size;
    LPSocketAddr addr;
}LPSocketMsg;


LP_API int
lpInitSocketContext ();

LP_API int
lpExitSocketContext ();

LP_API LPSocketAttacher*
lpNewSocketAttacher (LPService attach);

LP_API LPSocket
lpBindAddr(LPSocketAttacher* attacher,
    const char * host, uint16_t port, int protocol, int family);

LP_API int
lpListenSocket (LPSocketAttacher* attacher, LPSocket so, int backlog);

LP_API LPSocket
lpConnectAddr(LPSocketAttacher* attacher,
        const char* host, uint16_t port, LPSession session);

LP_API int
lpAcceptSocket (LPSocketAttacher* attacher,
                LPSocket so, LPSession session);

LP_API int
lpReadSocket (LPSocketAttacher* attacher,
    LPSocket so, size_t size, LPSession session);

LP_API int
lpSendSocket (LPSocketAttacher* attacher,
    LPSocket so, const char * buf, size_t size);

LP_API int
lpCloseSocket (LPSocketAttacher* attacher, LPSocket so);

LP_API int
lpForwardSocket (LPSocketAttacher* attacher,
                LPSocket so, LPSession session);

LP_API int
lpDispatchSocket(LPSocketAttacher* attacher);